/***************************************************************************
 *   Copyright (C) 2005 by xihe   *
 *   xihels@163.com                                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "ReceiveIM.h"
#include <iostream>
#include "CodeConverter.h"

using namespace std;


ReceiveIMPacket::ReceiveIMPacket(const uint8_t* buf, int len) :
    InPacket(buf, len)	// 这个包需要同步
{

    // 保存消息头和消息体的16字节，用来向服务器发送回复
    memcpy(reply_, buf, 16 + QQ_HEADER_LENGTH_BASIC_FAMILY_IN);

    const uint8_t* cur = buf + QQ_HEADER_LENGTH_BASIC_FAMILY_IN;
    // 消息头
    int imheaderlen = imHeader.getBody(cur, len);
    cur += imheaderlen;
    int bodybuflen = len - imheaderlen;


    string ou;
    int bodylen = 0;
    switch (imHeader.getMessageSource()) {  // 判断消息类型
	case QQ_RECV_IM_FROM_FRIEND:
	case QQ_RECV_IM_FROM_STRANGER:	    /* 是从好友或者陌生人处发来的消息 */
	    bodylen = parseNormalIM(cur, bodybuflen);
	    break;
	case QQ_RECV_IM_UNKNOWN_CLUSTER_IM:
	case QQ_RECV_IM_TEMP_CLUSTER_IM:
	case QQ_RECV_IM_CLUSTER_IM:
	    bodylen = parseClusterIM(cur, bodybuflen);
	    break;
    }

    if (imheaderlen + bodylen > len)
	throw runtime_error("ReceiveIMPacket Error: BUFFER len not enough");
}

int ReceiveIMPacket::parseNormalIM(const uint8_t* buf, int len) {
    const uint8_t* cur = buf;
    normalIMHeaderPtr.reset(new NormalIMHeader);
    int headerlen = normalIMHeaderPtr->getBody(cur, len);
    cur += headerlen;
    len -= headerlen;
    normalIMBodyPtr = NormalIMBodyPtr(new NormalIMBody);
    cur += normalIMBodyPtr->getBody(cur, len);
    return cur - buf;
}

int ReceiveIMPacket::parseClusterIM(const uint8_t* buf, int len) {
    const uint8_t* cur = buf;
    clusterIMBodyPtr.reset(new ClusterIMBody(imHeader.getMessageSource()));
    cur += clusterIMBodyPtr->getBody(cur, len);
    return cur - buf;
}
